Re: [SQL] Finding the "most recent" rows

Поиск
Список
Период
Сортировка
От Herouth Maoz
Тема Re: [SQL] Finding the "most recent" rows
Дата
Msg-id l03130300b34ddda65755@[147.233.159.109]
обсуждение исходный текст
Ответ на Re: [SQL] Finding the "most recent" rows  (Chris Bitmead <chris.bitmead@bigfoot.com>)
Список pgsql-sql
At 07:39 +0300 on 23/04/1999, Chris Bitmead wrote:


> httpd=> select title, summary, time from story t where time IN (select
> max(s.time) from story s GROUP BY s.title);
> ERROR:  parser: Subselect has too many or too few fields.

Of course it does. Since you group by a field which is not in the select
list, Postgres adds it silently to the fields to be selected. Thus the
subselect has two fields in each row, not just a single time field.

I think the proper syntax by the standard would be something like:
SELECT title, summary, timeFROM story tWHERE (time, title) IN (   SELECT s.title, max( s.time )   FROM story s   GROUP
BYs.title);
 

But I'm not sure Postgres even supports this format (of comparing against
several fields).

In any case, the best would be to select just one tuple in the subselect
and have it return only the time, by constraining the title. But
syntactically, the above is what you were trying to do.

Anyway, Postgres adds the group field to the query, which the standard
requires and common practice doesn't. I think perhaps after doing the
grouping, Postgres should drop that field, since it wasn't originally
requested.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma




В списке pgsql-sql по дате отправления:

Предыдущее
От: Mark Jewiss
Дата:
Сообщение: Re: [SQL] LIMIT
Следующее
От: Herouth Maoz
Дата:
Сообщение: Re: [SQL] Finding the "most recent" rows